package com.cp.yn.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.view.ViewCompat
import com.common.base.tools.extend.dp2px
import com.common.base.tools.extend.sp2px

/**
 * 三疯
 * *************************
 * 自定义View
 * *************************
 */
class CircleImageView : View {
    private var mCirclePaint = Paint()
    private var mArcPaint = Paint()
    private var mTextPaint = Paint()
    private var widthSize = 0

    private var heightSize = 0
    private var lineWidth = dp2px(5).toFloat()

    private var angle = -90f

    fun setProgress(angle:Float){
        this.angle = angle
        ViewCompat.postInvalidateOnAnimation(this)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        mCirclePaint.isAntiAlias = true
        mCirclePaint.style = Paint.Style.STROKE
        mCirclePaint.strokeWidth = lineWidth
        mCirclePaint.color = Color.BLACK

        mArcPaint.isAntiAlias = true
        mArcPaint.style = Paint.Style.FILL_AND_STROKE
        mArcPaint.strokeWidth = lineWidth
        mArcPaint.color = Color.RED

        mTextPaint.isAntiAlias = true
        mTextPaint.style = Paint.Style.STROKE
        mTextPaint.strokeWidth = dp2px(1).toFloat()
        mTextPaint.textSize = sp2px(12).toFloat()
        mTextPaint.color = Color.BLACK


    }

//    override fun onDraw(canvas: Canvas?) {
//        super.onDraw(canvas)
//        canvas?.let {
//            val radius = widthSize/2f-lineWidth
//            canvas.drawCircle(widthSize/2f,heightSize/2f, radius, mCirclePaint)
//
//            val mBound = RectF(lineWidth*2,lineWidth*2,radius*2,radius*2)
//            canvas.drawArc(mBound,0f,angle,true,mArcPaint)
//
//            canvas.drawText("${(angle/360*100).toInt()}%",widthSize/2f,heightSize/2f,mTextPaint)
//        }
//    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        widthSize = MeasureSpec.getSize(widthMeasureSpec)
        heightSize = MeasureSpec.getSize(heightMeasureSpec)
    }
}